<?php

Raise::load('core.RaiseServer');

/**
 * RaiseObject abstract class
 * This is the base class for all phpRaise classes
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core
 * @since 1.2
 * @abstract
 */
abstract class RaiseObject {
    
    /**
     * Create a class instance with parameters and properties
     * @param string $class The name of the class to create.
     * @param array|RaiseCollection $parameters The parameters for the constructor of the class
     * @param array|RaiseCollection $properties The properties to set through getter/setter functions
     * @return object
     */
    public static function _new($class, $parameters = null, $properties = null){
        $object = null;
        $reflection = new ReflectionClass($class);
        switch(func_num_args()){
            case 1:
                $object = $reflection->newInstance();
                break;
            case 2:
                $object = $reflection->newInstanceArgs($parameters);
                break;
            default:
                $object = $reflection->newInstanceArgs($parameters);
                foreach($properties as $key => $prop){
                    if(method_exists($object, $key)){
                        $object->$key($prop);
                    }elseif(property_exists($object, $key)){
                        $object->$key = $prop;
                    }
                }
                break;
        }
        return $object;
    }

    /**
     * Get the hash code of this object
     * @return string
     */
    public function hashCode() {
        if (RaiseServer::version()->compareTo('5.2', '<')) {
            ob_start();
            var_dump($this);
            $dump = ob_get_contents();
            ob_end_clean();
            $match = array();
            if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
                return sha1($match[1] . $match[2]);
            }
            return null;
        } else {
            return spl_object_hash($this);
        }
    }

    /**
     * Compare whether both objects are equal or not
     * @param RaiseObject $o
     * @return boolean
     */
    public function equals($o) {
        return $this->hashCode() === $o->hashCode();
    }

    /**
     * Dispose all resources and free memory associated with this object
     */
    public function dispose() {
        
    }
    
    public function properties(){
        return get_object_vars($this);
    }

    /**
     * Compatibility for OOP before PHP 5 for object cloning.
     * Note that in PHP 5 the clone provided is only a shallow copy.
     * @return object
     */
    public function raiseClone() {
        if (version_compare(PHP_VERSION, '5.0.0', '<')) {
            return $this;
        } else {
            return clone $this;
        }
    }

    /**
     * RaiseObject is destroyed, do the appropriate disposal
     */
    public function __destruct() {
        $this->dispose();
    }

}